import java.sql.*;
import java.util.HashMap;

//import com.mysql.jdbc.PreparedStatement;

public class Database
{
	//����Connection����
	private Connection con;

    public Database(String dbname)
    {
    	//����������
    	String driver = "com.mysql.jdbc.Driver";
    	//URLָ��Ҫ���ʵ����ݿ���mydata
    	String url = "jdbc:mysql://localhost:3306/"+dbname;
    	//MySQL����ʱ���û���
    	String user = "root";
         //MySQL����ʱ������
    	 String password = "";

    	 try
    	 {
			 Class.forName(driver);
			 con = (Connection)DriverManager.getConnection(url,user,password);
		 }
    	 catch (Exception e)
    	 {
			// TODO: handle exception
    		 e.printStackTrace();
		 }
    }

    public boolean insert(String tablename,HashMap map)
    {
    	String sql = "insert into "+tablename+" (id,password) values(?,?)";
    	PreparedStatement pstmt;

    	try
    	{
			pstmt = (PreparedStatement)con.prepareStatement(sql);
			pstmt.setString(1, map.get("id").toString());
			pstmt.setString(2,map.get("password").toString());
			int i = pstmt.executeUpdate();
		} catch (SQLException e)
		{
			// TODO: handle exception
			e.printStackTrace();
			try {
				con.rollback();
			} catch (SQLException e1) {
				// TODO �Զ����ɵ� catch ��
				e1.printStackTrace();
			}
			return false;
		}

    	return true;
    }

    public ResultSet select(String tablename,String colname)
    {
    	String sql = "select "+colname+" from "+tablename;
    	PreparedStatement pstmt;
    	ResultSet rs = null;

    	try
    	{
			pstmt = (PreparedStatement)con.prepareStatement(sql);
			rs = pstmt.executeQuery();
		}
    	catch (SQLException e)
    	{
			// TODO �Զ����ɵ� catch ��
			e.printStackTrace();
		}

    	return rs;
    }
}
